fix: preserve the context state during evaluate — fixes #90, #93#103
Merged
uw4 merged 4 commits intodashjoin:mainfrom Apr 29, 2026
Merged
fix: preserve the context state during evaluate — fixes #90, #93#103uw4 merged 4 commits intodashjoin:mainfrom
uw4 merged 4 commits intodashjoin:mainfrom
Conversation
…n#90, dashjoin#93) Replace the broken static ThreadLocal<Jsonata> with a lightweight EvalContext value object on a single static ThreadLocal. The Jsonata instance is now immutable after construction and freely shareable across threads with no cloning.
This was referenced Mar 28, 2026
Contributor
Author
|
@uw4 @aeberhart could you please take a look? |
Contributor
|
Thanks for the excellent contribution + providing the test cases, this is an excellent result! However we try to minimize the # of changes/changed lines of code, and also try to stay structurally as near as possible with the original Javascript. Thus while reproducing your work, it turned out that this is the only change required in order to fix the issues and pass all test cases (and also has no material effect on any benchmarks):
|
Reverted changes not reqd anymore
This was
linked to
issues
Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two related thread-safety bugs make it unsafe to cache and reuse parsed
Jsonatainstances:Issue #93 — Bindings leak across instances on the same thread
The constructor sets
current.set(this)on a singlestatic ThreadLocal<Jsonata>. When a second instance is constructed on the same thread, it overwrites the first. Subsequent evaluations of the first instance can silently use the second instance's per-thread evaluator state, causing environment bindings to leak across unrelated expressions.Issue #90 —
$eval()returnsnullfor nested/deep contexts_evaluate()stores evaluation state such asinputandenvironmentas mutable instance fields during recursive evaluation. When$eval()reads that state throughJsonata.current.get(), it can observe whatever the most recent recursive_evaluate()call wrote, rather than the correct calling context for that$eval()invocation. This causes cases like$eval($.funcs.func)to returnnullinstead of the expected result.Both bugs stem from the same root cause: per-evaluation mutable state stored on the shared
Jsonatainstance and accessed through a single staticThreadLocal<Jsonata>.Solution (minimal fix)
Preserve the context during evaluate on the stack.
Thus we can keep the original code structure and minimize the # of changed lines of code
Original Solution (not merged - used minimal fix)
Extract all per-evaluation mutable state into a lightweight
EvalContextvalue object stored in a singlestatic ThreadLocal<EvalContext>.This separates:
JsonataAs a result,
Jsonatano longer stores per-evaluation mutable state, and parsed expressions can be safely cached and reused across threads after setup.What changed
Jsonata.javastatic ThreadLocal<Jsonata> currentstatic ThreadLocal<EvalContext> evalContextJsonata(input,timestamp, current eval state)EvalContextgetPerThreadInstance()+ clone per threadJsonata(Jsonata other)current.set(this)_evaluate()overwrites mutable state onthis_evaluate()saves/restores state onEvalContextintry/finallyevaluate(Object, Frame)does not manage a dedicated execution contextevaluate(Object, Frame)creates/restoresEvalContextFunctions.javaJsonata.current.get()call sites now read fromJsonata.evalContext.get()$eval(),$now(),$millis(), and lambda application now useEvalContextfuncApply()no longer performs redundant repeatedThreadLocal.get()callsDesign